#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/8/21 17:13
# ===========================================
#       题目名称： 20. 有效的括号
#       题目地址： https://leetcode.cn/problems/valid-parentheses/
#       题目描述： https://note.youdao.com/s/RRXNSXjR
# ===========================================

import re


class Solution:
    def isValid(self, s):
        small = "()"
        middle = "[]"
        big = "{}"
        for i in range(len(s) // 2):
            s = s.replace(small, "").replace(middle, "").replace(big, "")
        return len(s) is 0

    def isValid2(self, s):
        """
            效率不行
        """
        kh, zkh, dkh = "()", "[]", "{}"
        while re.findall(r".*\(\)|\[\]|\{\}.*", s):
            if s.find(kh) != -1:
                s = s.replace(kh, "")
            elif s.find(zkh) != -1:
                s = s.replace(zkh, "")
            else:
                s = s.replace(dkh, "")
        return len(s) is 0


if __name__ == '__main__':
    s = Solution()
    # True
    print(s.isValid(s="()"))
    # True
    print(s.isValid(s="()[]{}"))
    # False
    print(s.isValid(s="(]"))
